// firstpages/information/index.js
const app = getApp() // 引入app
import _request from '../../utils/request.js'
// 引入SDK核心类，js文件根据自己业务，位置可自行放置
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk
const area = require('../../utils/area');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseurl: app.globalData.baseurl, //全局baseurl路径
    shopimgUrl: app.globalData.shopimgUrl,
    show: false,
    areaList: area.areaList,
    name: '',
    address: '',
    city: '',
    location: '',
    province: null,
    citydata: null,
    district: null,
    latitude: '',
    longitude: '',
    headImg: '',
    gender: 1, // 默认是男
  },

  // 男女
  radioChange(e) {
    console.log(e, 'e');
    this.setData({
      gender: e.detail.value
    })
  },
  // 头像
  afterRead1() {
    let _this = this
    wx.chooseImage({
      success(res) {
        console.log(res.tempFilePaths[0], 'res');
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: _this.data.baseurl + '/store-api/file/batchUpload',
          filePath: tempFilePaths[0],
          name: 'files',
          header: {
            'token': wx.getStorageSync('token') || null
          },
          formData: {
            'file': 'file'
          },
          success(res) {
            const headImg = JSON.parse(res.data).data[0]
            _this.setData({
              headImg
            })
          },
          fail(error) {
            console.log(error);
          },
          complete(res) {
            console.log(res);
          },
        })
      }
    })
  },

  getname(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getaddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  showmolde() {
    wx.navigateTo({
      url: '/firstpages/information/site/index',
    })
    // this.setData({
    //   show: true
    // });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onFirm(e) {
    console.log(e);
    this.setData({
      show: false,
      city: e.detail.values.map(item => {
        return item.name
      })
    });
  },
  onSave() {
    const {
      headImg,
      name,
      city,
      location,
      province,
      citydata,
      district,
      latitude,
      longitude,
      address,
      gender
    } = this.data
    if (!headImg) {
      wx.showToast({
        title: "请上传头像",
        icon: 'none'
      })
      return;
    }
    if (!name) {
      wx.showToast({
        title: "请输入门店名称",
        icon: 'none'
      })
      return;
    }
    if (!city) {
      wx.showToast({
        title: "请选择地区",
        icon: 'none'
      })
      return;
    }
    if (!address) {
      wx.showToast({
        title: "请输入详细地址",
        icon: 'none'
      })
      return;
    }

    // 保存个人信息
    _request.postObj('/storeUser/doUserData', {
      password: "123456",
      gender: gender,
      id: wx.getStorageSync('userid'),
      headPortrait: headImg
    }).then((res) => {
      // 保存门店信息
      _request.postObj('/storeUser/doStoreData', {
        cityGDId: citydata.areaId,
        districtGDId: district.areaId,
        latitude: latitude,
        location: address,
        longitude: longitude,
        mobile: wx.getStorageSync('user').mobile,
        provinceGDId: province.areaId,
        storename: name,
        userId: wx.getStorageSync('userid')
      }).then((res) => {
        if (res.data.code == 200) {
          wx.setStorageSync('storeId', res.data.data)
          wx.showToast({
            title: "申请成功，前往认证",
            icon: 'none',
          })
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/auth/index',
            })
          }, 2000);
        }
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})